﻿using System;
using System.Windows.Forms;
using Detective.View.Base;

namespace Detective.View.ConsoleViews.Base
{
    public class BaseChildView:IChildView
    {
        #region Fields

        private int
            _currentY;

        #endregion

        #region Properties

        public int TopMargin { get; set; }
        public int LeftMargin { get; set; }

        public int PositionX { get; set; }
        public int PositionY { get; set; }

        public BaseMdiView Parent { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }

        #endregion

        protected void ClearViewAndWriteBorder()
        {
            string border = new string('-', Width);
            string line = "|" + new string(' ', Width - 2) + "|";

            Console.SetCursorPosition(PositionX, PositionY);

            Console.WriteLine(border);

            for (int y = 1; y < Height - 1; y++)
            {
                Console.SetCursorPosition(PositionX, PositionY + y);
                Console.WriteLine(line);
            }

            Console.SetCursorPosition(PositionX, PositionY + Height - 1);
            Console.WriteLine(border);

            _currentY = PositionY;
        }

        public void ClearView()
        {
            string line = new string(' ', Width);

            for (int y = 0; y < Height; y++)
            {
                Console.SetCursorPosition(PositionX, PositionY + y);
                Console.WriteLine(line);
            }

            _currentY = PositionY;
        }

        protected void WriteLine()
        {
            WriteLine(string.Empty);
        }

        protected void WriteLine(string message, char toEnd)
        {
            var width = Width - LeftMargin*2;

            WriteLine(message + new string(toEnd, width - message.Length));
        }

        protected void WriteLine(string message)
        {
            int width = Width - LeftMargin*2;

            if (message.Length > width)
            {
                for (int i = 0; i < (int)Math.Round((double)(message.Length/width) + 0.5, MidpointRounding.AwayFromZero); i++)
                {
                    WriteLine(message.Substring(i*width, Math.Min(width, message.Length - i*width)));
                }
            }
            else
            {
                Console.SetCursorPosition(PositionX + LeftMargin, _currentY + TopMargin);

                Console.WriteLine(message);

                _currentY++;
            }
        }

        protected void Write(string message)
        {
            Console.SetCursorPosition(PositionX + LeftMargin, _currentY + TopMargin);

            Console.Write(message);
        }

        protected void WriteToRight(string message)
        {
            Console.SetCursorPosition(PositionX + Width - LeftMargin - message.Length, _currentY + TopMargin);

            Console.Write(message);
        }

        public virtual void OnKeyDown(KeyEventArgs arg)
        {
        }

        public virtual void OnKeyUp(KeyEventArgs arg)
        {
        }

        public virtual void RefreshView()
        {
        }
    }
}
